package co.fitstart.entity.coach;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicUpdate;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.utils.DateFormatUtils;
import co.fitstart.entity.utils.DateUtils;

/**
 * 
 * @author mohanlan
 *
 */
@Entity
@Table(name = "coach_rest_calendar")
@DynamicUpdate
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class CoachRestCalendar {
    
    @Id
    @GeneratedValue
    private Long id;
    
    /** 教练 */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private Principal coach;
    
    @Enumerated(EnumType.ORDINAL)
    private CoachRestType coachRestType;
    
    /** 休息日 */
    @org.hibernate.annotations.Type(type = "date")
    private Date restDate;
    
    /** 休息时间 */
    private String restHour;
    
    /** 后台排体验课 */
    private boolean adminExperience;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /*
     * default constructor
     */
    public CoachRestCalendar() {
    
    }
    
    /**
     * 
     * @param principal
     * @param coachRestType
     * @param day
     * @param hour
     * @param adminExperience
     */
    public CoachRestCalendar(Principal principal, CoachRestType coachRestType, Date day, String hour, boolean adminExperience) {
        this.coach = principal;
        this.coachRestType = coachRestType;
        this.restDate = day;
        this.restHour = hour;
        this.adminExperience = adminExperience;
        this.disabled = false;
    }
    
    /**
     * 
     * @param principal
     * @param day
     * @param hour
     */
    public CoachRestCalendar(Principal principal, Date day, String hour, boolean adminExperience) {
        this.coach = principal;
        this.restDate = day;
        this.restHour = hour;
        this.adminExperience = adminExperience;
        this.disabled = false;
    }
    
    /**
     * 
     * @param principal
     * @param day
     * @param hour
     * @return
     */
    public CoachRestCalendar edit(Principal principal, Date day, String hour) {
        this.coach = principal;
        this.restDate = day;
        this.restHour = hour;
        return this;
    }
    
    /**
     * 
     * @param adminExperience
     * @return
     */
    public CoachRestCalendar editAdminExperience(boolean adminExperience) {
        this.adminExperience = adminExperience;
        return this;
    }

    /**
     * 
     * @return
     */
    public CoachRestCalendar repeatWeek() {
        this.restDate = DateUtils.addWeeks(this.restDate, 1);
        return this;
    }
    
    /**
     * 
     * @return
     */
    public CoachRestCalendar disable() {
        this.disabled = true;
        return this;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public String getBeginData() {
        String date = DateFormatUtils.format(this.restDate, "yyyyMMdd");
        String hour = StringUtils.replace(this.restHour, ":", "");
        return this.coach.getId().toString() + date + hour;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public String getEndData() {
        String date = DateFormatUtils.format(this.restDate, "yyyyMMdd");
        Date currentDate = DateUtils.parseDate(DateFormatUtils.format(this.restDate, "yyyy-MM-dd ") + this.restHour, "yyyy-MM-dd HH:mm");
        Date endDate = org.apache.commons.lang3.time.DateUtils.addMinutes(currentDate, 30);
        String hour = DateFormatUtils.format(endDate, "HHmm");
        return this.coach.getId().toString() + date + hour;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public String getDayhour() {
        
        if(this.restDate != null && StringUtils.isNotBlank(restHour)) {
            return DateFormatUtils.format(this.restDate) + "|" + this.restHour; 
        }
        
        return null;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public String getHour() {
        
        if(this.restDate != null) {
            return DateFormatUtils.format(this.restDate) + "|" + this.restHour; 
        }
        
        return null;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public Long getCoachId() {
        return this.coach.getId();
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public boolean isRest() {
        return this.coachRestType.equals(CoachRestType.REST);
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public boolean isExperience() {
        return this.coachRestType.equals(CoachRestType.EXPERIENCE);
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public boolean isDuty() {
        return this.coachRestType.equals(CoachRestType.DUTY);
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public boolean isBusiness() {
        return this.coachRestType.equals(CoachRestType.BUSINESS);
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the coach
     */
    public Principal getCoach() {
        return coach;
    }

    /**
     * @return the coachRestType
     */
    public CoachRestType getCoachRestType() {
        return coachRestType;
    }

    /**
     * @return the restDate
     */
    public Date getRestDate() {
        return restDate;
    }

    /**
     * @return the restHour
     */
    public String getRestHour() {
        return restHour;
    }

    /**
     * @return the adminExperience
     */
    public boolean isAdminExperience() {
        return adminExperience;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param coach the coach to set
     */
    public void setCoach(Principal coach) {
        this.coach = coach;
    }

    /**
     * @param coachRestType the coachRestType to set
     */
    public void setCoachRestType(CoachRestType coachRestType) {
        this.coachRestType = coachRestType;
    }

    /**
     * @param restDate the restDate to set
     */
    public void setRestDate(Date restDate) {
        this.restDate = restDate;
    }

    /**
     * @param restHour the restHour to set
     */
    public void setRestHour(String restHour) {
        this.restHour = restHour;
    }

    /**
     * @param adminExperience the adminExperience to set
     */
    public void setAdminExperience(boolean adminExperience) {
        this.adminExperience = adminExperience;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
    
}
